package com.internship.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.internship.model.DepartmentInfo;
import com.internship.model.News;
import com.internship.page.Page;
import com.internship.service.Const;
import com.internship.service.DepartmentInfoService;
import com.internship.service.NewsService;
import com.internship.util.StringUtil;


@Controller
@RequestMapping("news")
public class NewsController {
	private final Logger logger = Logger.getLogger(NewsController.class);
	
	@Autowired
	private NewsService newsService;
	@Autowired
	private DepartmentInfoService 	departmentInfoService;
	public static HttpSession getSession() {
		HttpSession session = null;
		try {
			session = getRequest().getSession();
		} catch (Exception e) {
		}
		return session;
	}

	public static HttpServletRequest getRequest() {
		ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes();
		return attrs.getRequest();
	}
	private void clearSession() {
		getSession().invalidate();
	}
	
	@RequestMapping("/news_list.do")
	public ModelAndView getNewsList() {
		return getNewsList(null,null,1);
	}
	
	private List<DepartmentInfo>  departmentInfoList = null;
	@RequestMapping("/news_page_list.do")
	public ModelAndView getNewsList(@RequestParam String DeptCode,@RequestParam String Title,@RequestParam int pageNumber) {
		ModelAndView mv = new ModelAndView("news");
		HttpSession session = getSession();
		try {
			if (String.valueOf(session.getAttribute(Const.USER_ROLE)).equals("2")||String.valueOf(session.getAttribute(Const.USER_ROLE)).equals("1")) {
				Page<News> pageList = newsService.getNewsList(DeptCode, Title, pageNumber);
				this.departmentInfoList = departmentInfoService.getDepartmentInfoList();
				mv.addObject("currentPage", pageNumber);
				mv.addObject("DeptCode", DeptCode);
				mv.addObject("totalPage", pageList.getTotalPage());
				mv.addObject("departmentInfoList",departmentInfoList);
				mv.addObject("totalCount", pageList.getTotalCount());
				mv.addObject("newsList", pageList.getResultList());
				System.out.println(pageList.getResultList().size()+"===========");
			}else {
				Page<News> pageList = newsService.getNewsList(String.valueOf(session.getAttribute(Const.USER_DEPARTMENT_CODE)), Title, pageNumber);
				mv.addObject("currentPage", pageNumber);
				mv.addObject("totalPage", pageList.getTotalPage());
				mv.addObject("totalCount", pageList.getTotalCount());
				mv.addObject("newsList", pageList.getResultList());
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		mv.addObject("Title",Title);
		return mv;
	}
	/**?
	 * 查看通知
	 * @param id
	 * @return
	 */
	@RequestMapping("/news_check.do")
	public ModelAndView newsCheck(@RequestParam("id")int  id) {
		ModelAndView mv = new ModelAndView("news_check");
		News  news = newsService.getNewsById(id);
		
		mv.addObject("news",news);
		return mv;
	}
	/**?
	 * 删除
	 * @param Id
	 * @return
	 */
		@RequestMapping("/delete.do")
		@ResponseBody
		public JSONObject delNews(@RequestParam int id){
			JSONObject json = new JSONObject();
			int  rel= newsService.delNews(id);
			json.put("result", rel > 0 ?"success":"error");
			 return json; 
		}
		/**?
		 * 添加通知
		 * @return
		 */
		@RequestMapping("/news_add.do")
		public ModelAndView newsAdd() {
			ModelAndView mv = new ModelAndView("news_add");
			this.departmentInfoList = departmentInfoService.getDepartmentInfoList();
			mv.addObject("departmentInfoList",departmentInfoList);
			return mv;
		}
		
		@RequestMapping("/news_save.do")
		@ResponseBody
		public JSONObject saveNews(@RequestParam("DeptCode")String  DeptCode,@RequestParam("Title") String Title, @RequestParam("Content") String Content){
			JSONObject json = new JSONObject();
			HttpSession session = getSession();
			News news = new News();
			news.setTitle(Title);
			news.setContent(Content);
			news.setDeptCode(DeptCode);
			if (!DeptCode.equals("00")) {
				DepartmentInfo departmentInfo = departmentInfoService.getDepartmentInfoByXYDM(DeptCode);
				news.setDisplayName(departmentInfo.getXYMC());
			}else {
				news.setDisplayName("全校");
			}
			news.setTeacherNumber(String.valueOf(session.getAttribute(Const.SESSION_ACCOUNT)));
			news.setPubTime(StringUtil.getStrDateTime());
			int  rel= newsService.addNews(news);
			json.put("result", rel > 0 ?"success":"error");
			 return json; 
		}
		/**?
		 * 手机端通知
		 * @return
		 */
		@RequestMapping("/moible_news_list.do")
		public ModelAndView getMoibleNewsList() {
			return getMoibleNewsList(null,null,1);
		}
		
		@RequestMapping("/moible_news_page_list.do")
		public ModelAndView getMoibleNewsList(@RequestParam String DeptCode,@RequestParam String Title,@RequestParam int pageNumber) {
			ModelAndView mv = new ModelAndView("moible_news");
			HttpSession session = getSession();
			if (!String.valueOf(session.getAttribute(Const.SESSION_ACCOUNT)).equals("null")) {
			try {
					Page<News> pageList = newsService.getNewsList(String.valueOf(session.getAttribute(Const.USER_DEPARTMENT_CODE)), Title, pageNumber);
					mv.addObject("currentPage", pageNumber);
					mv.addObject("totalPage", pageList.getTotalPage());
					mv.addObject("totalCount", pageList.getTotalCount());
					mv.addObject("newsList", pageList.getResultList());
					
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			}else {
				 mv.setViewName("login_moible");
		}
			mv.addObject("Title",Title);
			return mv;
		}
		@RequestMapping("/moible_news_check.do")
		public ModelAndView moibleNewsCheck(@RequestParam("id")int  id) {
			ModelAndView mv = new ModelAndView("moible_news_check");
			News  news = newsService.getNewsById(id);
			
			mv.addObject("news",news);
			return mv;
		}
}